package com.chen.recruit.controller;

import com.chen.recruit.common.annotation.Log;
import com.chen.recruit.common.api.CommonResult;
import com.chen.recruit.common.enums.BusinessType;
import com.chen.recruit.common.valid.AddGroup;
import com.chen.recruit.common.valid.UpdateGroup;
import com.chen.recruit.domain.Menu;
import com.chen.recruit.service.MenuService;
import lombok.RequiredArgsConstructor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "菜单管理 MenuController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/menu")
public class MenuController {

    private final MenuService menuService;

    @ApiOperation("新增菜单")
    @SaCheckPermission("system:menu:add")
    @Log(module = "系统管理-菜单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@Validated(AddGroup.class) @RequestBody Menu menu) {
        return menuService.addOrUpdateMenu(menu);
    }

    @ApiOperation("删除菜单")
    @SaCheckPermission("system:menu:delete")
    @Log(module = "系统管理-菜单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{menuId}")
    public CommonResult delete(@PathVariable Integer menuId) {
        return menuService.deleteMenuById(menuId);
    }

    @ApiOperation("更新菜单")
    @SaCheckPermission("system:menu:update")
    @Log(module = "系统管理-菜单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@Validated(UpdateGroup.class) @RequestBody Menu menu) {
        return menuService.addOrUpdateMenu(menu);
    }

    @ApiOperation("获取菜单详细信息")
    @SaCheckPermission("system:menu:update")
    @GetMapping("/{menuId}")
    public CommonResult getInfo(@PathVariable Integer menuId) {
        return CommonResult.success(menuService.getById(menuId));
    }

    @ApiOperation("查询菜单列表")
    @SaCheckPermission("system:menu:list")
    @GetMapping("/list")
    public CommonResult list(Menu menu) {
        return CommonResult.success(menuService.getMenusByCondition(menu));
    }

    @ApiOperation("获取当前登录用户菜单列表")
    @GetMapping
    public CommonResult getMenusByLoginUser() {
        return CommonResult.success(menuService.getMenusByLoginUser());
    }

}
